package com.example.qt.ptplatform.controller;

import com.example.qt.ptplatform.entity.Comments;
import com.example.qt.ptplatform.entity.CommentRequest;
import com.example.qt.ptplatform.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @PostMapping("/submit")
    public ResponseEntity<Map<String, Object>> submitComment(@RequestBody CommentRequest commentRequest) {
        Map<String, Object> response = new HashMap<>();

        if (commentRequest.getTorrentId() == null || commentRequest.getUserId() == null ||
                commentRequest.getContent() == null || commentRequest.getContent().isEmpty()) {
            response.put("success", false);
            response.put("message", "错误：缺少必要的字段！");
            return ResponseEntity.badRequest().body(response);
        }

        Boolean isAnonymous = "1".equals(commentRequest.getIsAnonymous());
        try {
            // 修改service方法返回commentId
            Integer commentId = commentService.saveComment(commentRequest.getTorrentId(), commentRequest.getUserId(),
                    commentRequest.getContent(), isAnonymous);

            response.put("success", true);
            response.put("message", "评论提交成功!");
            response.put("commentId", commentId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "错误：" + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PostMapping("/reply")
    public ResponseEntity<Map<String, Object>> submitReply(@RequestBody CommentRequest commentRequest) {
        Map<String, Object> response = new HashMap<>();

        if (commentRequest.getTorrentId() == null || commentRequest.getUserId() == null ||
                commentRequest.getContent() == null || commentRequest.getContent().isEmpty() ||
                commentRequest.getComCommentId() == null) {
            response.put("success", false);
            response.put("message", "错误：缺少必要的字段！");
            return ResponseEntity.badRequest().body(response);
        }

        Boolean isAnonymous = "1".equals(commentRequest.getIsAnonymous());
        try {
            Integer commentId = commentService.saveReply(
                    commentRequest.getTorrentId(),
                    commentRequest.getUserId(),
                    commentRequest.getContent(),
                    isAnonymous,
                    commentRequest.getComCommentId()
            );

            response.put("success", true);
            response.put("message", "回复提交成功!");
            response.put("commentId", commentId);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "错误：" + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PostMapping("/like")
    public String likeComment(@RequestParam Integer userId, @RequestParam Integer commentId) {
        try {
            commentService.likeComment(userId, commentId);
            return "点赞成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    @PostMapping("/unlike")
    public String unlikeComment(@RequestParam Integer userId, @RequestParam Integer commentId) {
        try {
            commentService.unlikeComment(userId, commentId);
            return "取消点赞成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    @GetMapping("/checkLike")
    public boolean checkLikeStatus(@RequestParam Integer userId, @RequestParam Integer commentId) {
        return commentService.checkLikeStatus(userId, commentId);
    }

    @DeleteMapping("/delete")
    public String deleteComment(@RequestParam Integer commentId) {
        try {
            commentService.deleteComment(commentId);
            return "评论删除成功!";
        } catch (Exception e) {
            return "错误：" + e.getMessage();
        }
    }

    @GetMapping("/listByTorrentId")
    public List<Comments> getCommentsByTorrentId(@RequestParam Integer torrentId) {
        try {
            return commentService.getCommentsByTorrentId(torrentId);
        } catch (Exception e) {
            throw new RuntimeException("获取评论列表失败: " + e.getMessage());
        }
    }
}